#coding=utf-8
import requests
import re
import time
import queue
from tkinter import *
import tkinter as tk
import tkinter.messagebox
from functools import reduce
import platform
import getpass
import threading
import os
from DokiLogin import *
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import openpyxl
from openpyxl import Workbook
if platform.system() == "Windows":
	doc_path = ''
else:
	doc_path = u"/Users/{}/Documents".format(getpass.getuser())
	try:
		os.path.join(doc_path, u'陈意涵')
		os.mkdir(doc_path+u'/陈意涵')
	except Exception as e:
		pass
	doc_path = doc_path+u'/陈意涵/'


def filt(url, count):
	# 判断时候有效
	if "http" not in url:
		return (False, 0, url)
	# 转化为pc_url
	try:
		id = re.search("id=[0-9]{11,14}", url, flags=0).group().split("=")[1]
	except:
		return (False, 0, url)
	# 判断帖子时候被删除
	print("判断帖子是否被删除")
	text = requests.get("https://v.qq.com/doki/doki_note/detail?starid=1661556&dataKey=starid%3D1661556%26ftid%3D51661556%26targetid%3D2657077965&noteid="+id).text
	if "empty_title" in text:
		print ("该帖子被删除")
		return (False, "已被删除", url)
	try:
		text = text.split("_comment_num\">")[1]
		text = text.split("</span>")[0]
	except Exception as e:
		# print(url)
		print("该条帖子评论数显示0条，直接返回")
		return (True, 0, url)

	
	print("该帖子未被删除")
	filt_url = "https://node.video.qq.com/x/api/doki_comment?&noteid={}&page={}"
	current_time = int(time.time())
	request_url = filt_url.format(id, current_time)	
	userIds = set()
	json = requests.get(request_url).json()
	# print(len(json["commentList"]))
	for comment in json["commentList"]:
		# print (comment["userInfo"]["actorId"])
		userIds.add(comment["userInfo"]["actorId"])
		current_time = comment["time"]
	# print(len(userIds))
	while json["hasNextPage"]:
	# while len(userIds) < count and json["hasNextPage"]:
		request_url = filt_url.format(id, current_time)	
		json = requests.get(request_url).json()
		for comment in json["commentList"]:
			userIds.add(comment["userInfo"]["actorId"])
			current_time = comment["time"]
	# print(len(userIds))
	if len(userIds) < count:
		print("该条帖子评论数未超过{}条".format(count))
		print("该条帖子界面上显示评论数为{}条".format(text))
		print("该条帖子的有效评论数为{}条".format(len(userIds)))
		print("当前帖子链接为：" + url)
		url = "https://v.qq.com/doki/doki_note/detail?starid=1661556&noteid=" + id + "&dataKey=starid%3D1661556%26ftid%3D51661556%26targetid%3D2657077965"	
		return (True ,len(userIds), url)
	else:
		print("该条帖子评论数超过{}条,当前评论数{}条".format(count,len(userIds)))
		return (False,len(userIds), url)

class FiltTalk(object):
	"""docstring for FiltTalk"""
	def __init__(self, view):
		self.is_start = False	# 开始
		self.urls = []			# 链接
		self.queue = queue.Queue()
		self.finish_total = 0
		self.all_total = 0
		self.valid_total = 0
		self.file_path_var = StringVar()
		self.key_var = StringVar()
		self.urls_list_var = StringVar()
		self.less_talk_count_var = StringVar()
		self.finish_total_var = StringVar()
		self.all_total_var = StringVar()
		self.valid_total_var = StringVar()
		self.setupUI(view)

	def setupUI(self, view):
		self.leftView = Frame(view, width=400,height=600)
		self.leftView.grid(rowspan=20)
		self.leftView.grid_propagate(0)
		Label(self.leftView, textvariable=self.file_path_var).grid()
		Listbox(self.leftView,listvariable=self.urls_list_var,width=55,height=28).grid(row=1)

		Button(view, text='开始', width = 12, height=1, command=self.start).grid(row=1,column=2,padx=5)
		Button(view, text='选择链接文本', width = 15, height=1, command=self.open_links).grid(row=1,column=3,padx=10)

		fieldView = Frame(view)
		fieldView.grid(row=2,column=2,columnspan=2,pady=5,padx=5)
		Label(fieldView,text='授权码：').grid(row=0,column=0, pady=5)
		Entry(fieldView, width = 15,textvariable=self.key_var).grid(row=0,column=1, pady=5)

		fieldView1 = Frame(view)
		fieldView1.grid(row=3,column=2,columnspan=2,pady=5,padx=5)
		Label(fieldView1,text='筛选评论数小于：').grid(row=0,column=0, pady=5)
		Entry(fieldView1, width = 3,textvariable=self.less_talk_count_var).grid(row=0,column=1, pady=5)
		self.less_talk_count_var.set("80")

		Label(view, textvariable=self.all_total_var).grid(row=5,column=2,columnspan=2)
		Label(view, textvariable=self.finish_total_var).grid(row=6,column=2,columnspan=2)
		Label(view, textvariable=self.valid_total_var).grid(row=7,column=2,columnspan=2)

		self.finish_total_var.set("当前完成：0条")
		self.all_total_var.set("链接总数：0条")
		self.valid_total_var.set("有效链接：0条")

		imgInfo = PhotoImage(file = '3.gif')
		lblImage = Label(view, image = imgInfo)
		lblImage.image = imgInfo
		lblImage.grid(row=14,column=2,sticky=S,columnspan=2)


	def start(self):
		# 判断授权码
		if self.key_var.get() != "cyh-19971017":
			tkinter.messagebox.showerror(u"温馨提示",u"请输入正确的授权码")
			self.tab3_start = False
			return
		if len(self.urls) == 0:
			tkinter.messagebox.showerror(u"温馨提示",u"请选择转化链接！！！！")
			self.tab3_start = False
			return
		if self.is_start == True: 
			tkinter.messagebox.showerror(u"温馨提示",u"脚本已经在运行，请勿重复操作")
			return
		count = 0	
		try:
			count = int(self.less_talk_count_var.get())
			
		except:
			count = 0
		if count <= 0:
			tkinter.messagebox.showerror(u"温馨提示",u"筛选评论数应是大于等于1的数字")
			return
		self.less_talk_count = count
		self.finish_total_var.set("当前完成：0条")
		self.valid_total_var.set("有效链接：0条")
		if tkinter.messagebox.askyesno(u"温馨提示",u"确定开始运行程序吗？"):
			self.is_start = True
			def loop():
				self.open_threads()
			loopThread = threading.Thread(target=loop, name='tab3')
			loopThread.setDaemon(True) 
			loopThread.start()

	def open_threads(self):
		self.finish_total = 0
		self.valid_total = 0
		urls = self.urls.copy()
		# 分割urls 
		urls = [urls[i:i + int(len(urls)/4)] for i in range(0, len(urls), int(len(urls)/4))]
		threads = []
		self.problem_urls = []
		self.finish_urls = []
		for index, url in enumerate(urls):
			def loop():
				self.work(url, index)
			loopThread = threading.Thread(target=loop, name='tab3_{}'.format(index))
			loopThread.setDaemon(True) 
			loopThread.start()
			threads.append(loopThread)
		for x in threads:
			x.join()

		# for url in self.problem_urls:
		# 	# print("将该帖子写入\"有问题或评论超过{}条.txt\"".format(self.less_talk_count))
		# 	oktalk = open(doc_path + u'有问题或评论超过{}条.txt'.format(self.less_talk_count), 'a')
		# 	oktalk.write(url)
		# 	if "\n" not in url:
		# 		oktalk.write("\n")
		# 	oktalk.close()
		if len(self.problem_urls) != 0:
			try:
				wb = openpyxl.load_workbook(doc_path + u'有问题或评论超过{}条.xlsx'.format(self.less_talk_count))
			except Exception as e:
				wb = Workbook()
			name = wb.sheetnames[0]
			sheet = wb[name]
			for url in self.problem_urls:
				sheet.append(url)
			wb.save(filename=doc_path + u'有问题或评论超过{}条.xlsx'.format(self.less_talk_count))
		

		if len(self.finish_urls) != 0:
			try:
				wb = openpyxl.load_workbook(doc_path + u'评论小于{}条.xlsx'.format(self.less_talk_count))
			except Exception as e:
				wb = Workbook()
			name = wb.sheetnames[0]
			sheet = wb[name]
			for url in self.finish_urls:
				sheet.append(url)
			wb.save(filename=doc_path + u'评论小于{}条.xlsx'.format(self.less_talk_count))	
			# pprint("将该帖子写入\"评论小于{}条.txt\"".format(self.less_talk_count))
			# oktalk = open(doc_path + u'评论小于{}条.txt'.format(self.less_talk_count), 'a')
			# oktalk.write(url)
			# oktalk.write("\n")
			# oktalk.close()
		self.is_start = False
		self.queue.put("stop")

	def open_links(self):
		if self.is_start == True:
			tkinter.messagebox.showerror(u"警告",u"脚本已经在执行，请等待执行结束")
			return 
		if tkinter.messagebox.askyesno(u"温馨提示",u"是否要删除之前导入的链接"):
			self.urls_list_var.set("")
			self.urls = []
		filename = tkinter.filedialog.askopenfilename(filetypes = [('TXT', 'txt')])
		if filename != '':
			urls_files = open(filename, 'r')
			urls = urls_files.readlines()
			# 去重操作
			urls = list(set(urls))
			for url in urls:
				temp = url.strip()
				if temp == '':
					urls.remove(url)

			for index, url in enumerate(urls):
				urls[index] = url.strip()
			urls = list(set(urls))
			#print urls
			self.urls.extend(urls)
			self.urls = list(set(self.urls))
			self.all_total = len(urls)
			self.all_total_var.set("链接总数：{}条".format(len(self.urls)))
			self.urls_list_var.set(tuple(self.urls))
		
	def work(self, urls, thread_index):
		
		
		print ("开始筛选")
		for index, url in enumerate(urls):
			print ("当前线程为第{}条，正在筛选第{}条，请耐心等待".format(thread_index,index+1))
			finish_url = filt(url, self.less_talk_count)

			if finish_url[0] == False:	##链接无效或者超出评论数
				#print("将该帖子写入\"有问题或评论超过{}条.txt\"".format(self.less_talk_count))
				self.problem_urls.append((finish_url[1], finish_url[2]))
			else:					##链接未超出要求评论数
				self.finish_urls.append((finish_url[1], finish_url[2]))
				self.valid_total += 1
				self.valid_total_var.set("有效链接：{}条".format(self.valid_total))
			self.finish_total += 1
			self.finish_total_var.set("当前完成：{}条".format(self.finish_total))
			print("第{}条链接已经筛选完成，当前有效链接数为{}条".format(index+1, self.valid_total))
			
		





